package com.lecyon.farm.util;

import com.lecyon.farm.common.constant.CommonConstant;
import lombok.Synchronized;
import org.apache.commons.lang3.RandomStringUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 系统租户工具类
 *
 * @author Yao Zheng
 * @Date 2019/11/15 11:11
 */
public class TenantUtils {

    private static final String CONNECTOR = "_";

    /**
     * 根据系统版权信息、租户编号（开通公司的编号)、开通时间、4位随机字符串 经过MD5加密
     * 生成tenantId
     *
     * @param tenantNumber
     * @param registerDate
     * @return
     */
    @Synchronized
    public static String createTenantId(String tenantNumber, LocalDateTime registerDate) {
        if (StringHelper.isEmpty(tenantNumber)) {
            return null;
        }
        if (ValidateUtils.isNullOrEmpty(registerDate)) {
            registerDate = LocalDateTime.now(ZoneId.systemDefault());
        }
        StringBuffer tenantId = new StringBuffer();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(CommonConstant.DATE_TIME_FILE_NAME_FORMAT_PATTERN);
        String formatRegisterDateTime = dateTimeFormatter.format(registerDate);
        tenantId.append(CommonConstant.COPYRIGHT).append(CONNECTOR)
                .append(tenantNumber).append(CONNECTOR)
                .append(formatRegisterDateTime).append(CONNECTOR)
                .append(RandomStringUtils.randomAlphanumeric(4));
        System.out.println(tenantId.toString());
        return EncryptionAndDecryptionUtils.md5Encryption(tenantId.toString());
    }

}
